package com.code.petapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.code.petapp.R;
import com.code.petapp.bean.HealthBean;
import com.code.petapp.bean.LostBean;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FosterAdapter extends RecyclerView.Adapter<FosterAdapter.MyViewHolder> {
    private Context context;
    private List<HealthBean> list;
    private View inflater;
    private OnItemClickListener mListener;
    private final ExecutorService executor;

    public FosterAdapter(Context context, List<HealthBean> list) {
        this.context = context;
        this.list = list;
        executor = Executors.newSingleThreadExecutor();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.item_foster, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }

    public interface OnItemClickListener {
        void onClick(View parent, int position);
    }

    // 设置点击事件
    public void setOnItemClickListener(OnItemClickListener l) {
        this.mListener = l;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        HealthBean healthBean = list.get(position);
        holder.name.setText(healthBean.getFosterName());
        holder.blood.setText(healthBean.getBlood());
        holder.temper.setText(healthBean.getTemper());
        holder.eating.setText(healthBean.getEating());
        holder.renal.setText(healthBean.getRenal());
        holder.time.setText(healthBean.getTime());
        executor.execute(() -> {
            Bitmap bitmap;
            try (MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever()) {
                String videoUrl = healthBean.getVideo();
                if (videoUrl.startsWith("http")) {
                    mediaMetadataRetriever.setDataSource(videoUrl, Collections.emptyMap());
                } else {
                    mediaMetadataRetriever.setDataSource(videoUrl);
                }
                bitmap = mediaMetadataRetriever.getFrameAtTime(10 * 1000 * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            // 更新UI必须在主线程中执行
            new Handler(Looper.getMainLooper()).post(() -> {
                if (bitmap != null) {
                    Glide.with(holder.itemView.getContext())
                            .load(bitmap)
                            .into(holder.imgVideo);
                }
            });
        });

        if (null != mListener) {
            holder.imgPlay.setOnClickListener(v -> mListener.onClick(holder.imgPlay, position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView blood;
        TextView temper;
        TextView eating;
        TextView renal;
        TextView time;
        ImageView imgVideo;
        ImageView imgPlay;

        public MyViewHolder(View itemView) {//这个view参数就是recyclerview子项的最外层布局
            super(itemView);
            //可以通过findViewById方法获取布局中的TextView
            name = itemView.findViewById(R.id.name);
            blood = itemView.findViewById(R.id.blood);
            temper = itemView.findViewById(R.id.temper);
            eating = itemView.findViewById(R.id.eating);
            renal = itemView.findViewById(R.id.renal);
            time = itemView.findViewById(R.id.time);
            imgVideo = itemView.findViewById(R.id.imgVideo);
            imgPlay = itemView.findViewById(R.id.imgPlay);
        }
    }
}
